package com.fionera.sample.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v4.app.Fragment
import com.wsdk.activity.WsdkActivity
import com.wsdk.view.ProcessDialog

@SuppressLint("Registered")
open class BaseActivity : WsdkActivity() {
    protected lateinit var mActivity: Activity
    protected lateinit var mProcessDialog: ProcessDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        setCanConnectDevice(false)
        setNoNeedDealWithResetKey(false)
        super.onCreate(savedInstanceState)

        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        mActivity = this
        mProcessDialog = ProcessDialog(this)
    }

    fun changeFragment(fragment: Fragment, first: Boolean, id: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        if (!first) {
            transaction.addToBackStack(null)
        }
        transaction.replace(id, fragment)
        transaction.commitAllowingStateLoss()
    }

    override fun onBackPressed() {
        val manager = supportFragmentManager
        if (manager != null && manager.backStackEntryCount > 0) {
            manager.popBackStackImmediate()
        } else {
            finish()
        }
    }
}
